package entity

import (
	"encoding/json"
	"strconv"

	"github.com/lab-online/internal/lab/valueobject"
)

func (l *Lab) SetupMimoDecodeContent() error {
	args := l.Arguments.(valueobject.MimoDecode)

	if l.Content == "" {
		content, _ := json.Marshal(map[string]any{
			"lab": "mimo",
			"decode": map[int]map[string]any{
				*args.Index: {"args": l.Arguments},
			},
		})

		l.Content = string(content)
		return nil
	}

	patch, _ := json.Marshal([]map[string]any{
		{
			"op":    "add",
			"path":  "/decode/" + strconv.Itoa(*args.Index),
			"value": map[string]any{"args": l.Arguments},
		},
	})

	return l.ApplyContentPatch(patch)
}

func (l *Lab) SetupMimoPlotContent() error {
	if l.Content == "" {
		content, _ := json.Marshal(map[string]any{
			"lab": "mimo",
			"plot": map[string]any{
				"args": l.Arguments,
			},
			"decode": map[int]any{},
		})

		l.Content = string(content)
		return nil
	}

	patch, _ := json.Marshal([]map[string]any{
		{
			"op":   "add",
			"path": "/plot",
			"value": map[string]any{
				"args": l.Arguments,
			},
		},
	})

	return l.ApplyContentPatch(patch)
}
